package com.se2.antiqueexhibition.Controller;


import com.se2.antiqueexhibition.Service.ReservationService;
import com.se2.antiqueexhibition.pojo.Activity;
import com.se2.antiqueexhibition.pojo.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping
public class ReservationControll {

    @Resource
    private ReservationService reservationService;

    @RequestMapping("/addreservation/{id}")
    public String addReservation(@PathVariable int id,HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");

        System.out.println(user.getId());
        reservationService.addReservation(user.getId(),id);
        List<Integer> reservations = reservationService.getReservationIdsByUserId(user.getId());
        session.setAttribute("reservations", reservations);
        return "redirect:/event";
    }

    @RequestMapping("/reservation")
    public String reservationList(HttpSession session,ModelMap model) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");
        List<Activity> activities = reservationService.showReservation(user.getId());
        System.out.println(activities);
        model.addAttribute("activities", activities);
        return "reservation";
    }

    @RequestMapping("/cancelreservation/{id}")
    public String cancelReservation(@PathVariable int id,HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");
        System.out.println(user.getId());
        reservationService.cancelReservation(user.getId(), id);
        List<Integer> reservations = reservationService.getReservationIdsByUserId(user.getId());
        session.setAttribute("reservations", reservations);
        System.out.println(reservations);
        return "redirect:/reservation";
    }

}
